<?php
/*
Copyright 2007,2008 Benjamin V King

BH Widgets is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.

BH Widgets is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with BH Widgets; if not, write to the Free Software Foundation,
Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
*/

require_once('BH_Taxonomy_Widget.php');

class BH_Tag_Dropdown_Widget extends BH_Taxonomy_Widget {

	function __construct() {
		parent::__construct('post_tag');
	}
		
	function get_tags($where='') {
		return $this->get_taxonomies($where);
	}

	function widget($args) {
		global $wpdb;
		extract($args);
		
		$all = $this->get_attribute('all');
		$selected_tags = $this->get_attribute('selected-tags');
		$message = $this->get_attribute('message');
		
		if (!$selected_tags && !$all) return;

		echo "$before_widget\n";
		echo $this->get_widget_start_tag($args);

		echo "<form action=\"\" method=\"\" name=\"tagForm\" >\n";
		echo "<select name=\"tagURL\" onchange=\"window.location=(document.forms.tagForm.tagURL[document.forms.tagForm.tagURL.selectedIndex].value);\" >\n";
		echo "<option value=\"\">$message</option>\n";
		
		foreach($this->get_tags() as $tag) {
			if (!$all && !in_array($tag->term_id, $selected_tags))
				continue;
		
			$select =  $tag->term_id == get_query_var('tag') ? " selected=\"true\"" : "";
			echo "<option value=\"". get_tag_link($tag->term_id) ."\"$select>";
			echo $tag->name ."</option>\n";
		}

		echo "</select>\n";
		echo "</form>\n";
		
		echo $this->get_widget_end_tag($args);
		
		echo "$after_widget\n";
	}

	function control() {
		if ( $this->get_request_param_value('submit') ) {
			$this->update_attribute_from_param('selected-tags');
			$this->update_attribute_from_param('all');
			$this->update_attribute_from_param('message');
		}
		
		$selected_tags = $this->get_attribute('selected-tags');
		$all = $this->get_attribute('all');
		$message = $this->get_attribute('message');
		
		echo "<label>Message<input type=\"text\" name=\"". $this->get_request_param_name('message') ."\" value=\"". $message ."\"/></label><br/>\n";
		echo "<label>All:</label><input type=\"checkbox\" name=\"" . $this->get_request_param_name('all') . "\" ". ($all=='1' ? 'checked' : '') ." value=\"1\"/><br/>\n";
		echo "<select name=\"" . $this->get_request_param_name('selected-tags') . "[]\" size=\"5\" multiple >\n";

		foreach($this->get_tags() as $tag) {
			$selected='';
			if (in_array($tag->term_id, $selected_tags)) {
				$selected='selected="true"';
			}

			echo "<option value=\"". $tag->term_id ."\" ". $selected .">". $tag->name ."</option>\n";
		}

		echo "</select>\n";
		echo "<input type=\"hidden\" id=\"" . $this->get_request_param_name('submit') . "\" name=\"" . $this->get_request_param_name('submit') . "\" value=\"1\" />\n";
	}
}

bh_widgets_register_class('BH_Tag_Dropdown_Widget');
?>
